﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.UI;
using OnlineVideos.Common;
using OnlineVideos.Data;
using Common.Library;
using OnlineVideos.View_Models;


using Common.Utilities;
using System.Windows.Navigation;
using MyToolkit.Multimedia;
using System.ComponentModel;
using OnlineVideos.Entities;
using System.Threading.Tasks;
using Microsoft.Phone.Shell;

namespace OnlineMovies.Views
{
    public partial class VideoGamesDetail : PhoneApplicationPage
    {
        string background = "0";
        public VideoGamesDetail()
        {
            InitializeComponent();
            if (AppSettings.ShowID != "0")
            {
                LoadPivotThemes(AppSettings.ShowUniqueID);
                //TODO: Save title also in settings or pass through query string
                tblkVideosTitle.Text = OnlineShow.GetShowDetail(AppSettings.ShowUniqueID).Title;
                background = "1";
                if (!ShowCastManager.ShowGamePivot(AppSettings.ShowID))
                {
                    pvtMainDetails.Items.Remove(gamepivot);
                }
            }
           Loaded += new RoutedEventHandler(PhoneApplicationPage_Loaded);

        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //int showid = AppSettings.ShowUniqueID;
            //string sharedstatus = Task.Run(async () => await Constants.connection.Table<ShowList>().Where(i => i.ShowID == showid).FirstOrDefaultAsync()).Result.ShareStatus;
            //if (sharedstatus == "Shared To Blog")
            //    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = "share this" + " " + AppResources.Type + "(" + "Shared To Blog)";
            //else
            //    ((ApplicationBarMenuItem)ApplicationBar.MenuItems[0]).Text = "share this" + " " + AppResources.Type + "(" + "Not Shared)";
            try
            {
                FlurryWP8SDK.Api.LogEvent("VideoGamesDetail Page", true);
                performanceProgressBar.IsIndeterminate = true;

                string id = "";
                if (NavigationContext.QueryString.TryGetValue("id", out id))
                    AppSettings.ShowID = id;

                if (background == "0")
                {
                    LoadPivotThemes(AppSettings.ShowUniqueID);
                    tblkVideosTitle.Text = OnlineShow.GetShowDetail(AppSettings.ShowUniqueID).Title;
                }
                performanceProgressBar.IsIndeterminate = false;
                PageHelper.RemoveEntryFromBackStack("VideoGameDetail");
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In VideoGamesDetail.cs file.", ex);
            }
        }
        #region Private Methods
       private void LoadPivotThemes(long ShowID)
        {
            pvtMainDetails.Background = ShowDetail.LoadShowPivotBackground(ShowID);
           
        }
        #endregion
        private void imgTitle_MouseEnter(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("VideoGamesDetail Page");

            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In VideoGamesDetail.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
               
                FlurryWP8SDK.Api.LogPageView();
                if (VideosList.lbxSongsList.IsEnabled == false)
                    VideosList.lbxSongsList.IsEnabled = true;
                YouTube.CancelPlay();
            }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In VideoGamesDetail.cs file.", ex);
            }
        }
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            if (YouTube.CancelPlay())
                e.Cancel = true;

            base.OnBackKeyPress(e);
        }
    }
}